package com.helloandroid.activitys

import android.widget.TextView
import com.helloandroid.R
import com.helloandroid.ads.SmallNativeAdUtil
import com.helloandroid.base.BaseActivity
import com.helloandroid.dialogs.BodyHelpDialog
import com.helloandroid.tools.MyLog
import com.helloandroid.view.MySpannableString
import com.helloandroid.view.PgData
import com.helloandroid.view.PgView

class BodyResultActivity : BaseActivity() {

    lateinit var tvResult:TextView
    lateinit var pgBMI:PgView
    lateinit var pgTz:PgView

    override fun getContentViewResId() = R.layout.activity_body_result

    override fun initView() {
        tvResult = findViewById(R.id.tvResult)
        pgBMI = findViewById(R.id.pgBMI)
        pgTz = findViewById(R.id.pgTz)

        findBtn(R.id.ibBack).setOnClickListener { finish() }
        findBtn(R.id.btnRecheck).setOnClickListener { finish() }
        findBtn(R.id.btnHelp).setOnClickListener {
            BodyHelpDialog(this).show()
        }

        SmallNativeAdUtil.showNativeAd(this,findViewById(R.id.nativeView))
    }

    override fun initData() {

        val bmi = intent.getFloatExtra("bmi",20f)
        val bmi_state = intent.getStringExtra("bmi_state")
        val tz = intent.getFloatExtra("tz",10f)
        val sex = intent.getIntExtra("sex",0)

        //------------------------------------------
        MyLog.elog("====================== info===================")
        MyLog.elog("bmi:$bmi")
        MyLog.elog("bmi_state:$bmi_state")
        MyLog.elog("tz:$tz")
        MyLog.elog("sex:$sex")

        //-------------------------------------------
        // bmi data
        val bmiData = PgData(
                min = 10f,
                max = 40f,
                mid1 = BodyInfoActivity.BMI_Min,
                mid2 = BodyInfoActivity.BMI_Max,
                mid3 = BodyInfoActivity.BMI_Super,
                cur = bmi,
                texts = arrayListOf("偏瘦","理想","偏胖","肥胖")
        )
        pgBMI.updatePgData(bmiData)

        // 女性20%～25%,男性15%～18%
        // 计算体脂
        val tzData:PgData
        if(sex == 0){
            // 女性
            tzData = PgData(
                    min = 5f,
                    max = 50f,
                    mid1 = 20f,
                    mid2 = 25f,
                    mid3 = 30f,
                    cur = tz,
                    texts = arrayListOf("偏瘦","标准","微胖","肥胖")
            )
        }else{
            // 男性
            tzData = PgData(
                    min = 5f,
                    max = 50f,
                    mid1 = 20f,
                    mid2 = 25f,
                    mid3 = 30f,
                    cur = tz,
                    texts = arrayListOf("偏瘦","标准","微胖","肥胖")
            )
        }
        pgTz.updatePgData(tzData)

        // 偏瘦 一般 理想 微胖 肥胖
        val bmiIdx = bmiData.curIdx
        val tzIdx = tzData.curIdx
        val total = bmiIdx + tzIdx
        val value = when{
            total <= 1 -> "偏瘦"
            bmiIdx == 1 && tzIdx == 1 -> "理想"
            bmiIdx > 2 && tzIdx > 2 -> "肥胖"
            (bmiIdx == 2 && tzIdx >= 2) || (bmiIdx >= 2 && tzIdx == 2) -> "微胖"
            else -> "一般"
        }

        // 计算健康状态
        val content  = MySpannableString(this,"根据你的基础个人信息,您的健康状态$value,体重和体脂偏离健康范围. 跳绳可以锻炼心脏功能、锻炼肌肉,跳绳15分钟相当于跑步30分钟,建议每天跳2-3组,每组60-100跳,间隔1分钟.")
                .first("您的健康状态$value")
                .textColor(R.color.dd_main_blue)
                .underline()
        tvResult.text = content
    }
}